package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.dto.*;
import com.itheima.entity.Houses;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.itheima.service.IHousesService;

import java.io.IOException;
import java.util.List;

/**
* houses 相关api
* @author Wei
* @since 2021-8-6 15:43:51
*/
@RestController
@RequestMapping("/houses")
public class HousesController{

@Autowired
private IHousesService housesService;

@RequestMapping("/list")
    private ResponseEntity houseList(String areaId){
    try {

        List<HouseDTO> list = housesService.houseList(areaId);
        return ResponseEntity.ok(list);
    }catch (Exception e){
        return ResponseEntity.ok(e);
    }

    }
    @RequestMapping("house")
    private ResponseEntity houseDetail(int houseId,String areaName){
        HouseRequest houseRequest = new HouseRequest();
        houseRequest.setHouseId(houseId);
        houseRequest.setAreaName(areaName);
       try {
            HouseDetailDTO houseDetail = housesService.selectHouseDetailById ( houseRequest);
           return ResponseEntity.ok(houseDetail);
       }catch (Exception e){
           return ResponseEntity.ok(e);
       }
    }
    @PostMapping("/selectByFilter")
    private ResponseEntity selectByFilter(@RequestBody HouseFilter houseFilter ,@RequestParam(required = true,defaultValue = "1") String pageSize) throws IOException {
        int i = Integer.parseInt(pageSize);
        IPage<HouseDTO>  iPage= housesService.selectHouseByFileter(houseFilter,i);

        return ResponseEntity.ok(iPage);
    }
    @GetMapping("/selectByid")
    private ResponseEntity selecttByCityName(int houseId) throws IOException {
        String s = housesService.selectCityById(houseId);
        return ResponseEntity.ok(s);

    }
}